package com.hexinfo.dmpro.monitor.service;

import com.alibaba.fastjson.JSONObject;
import com.hexinfo.dmpro.common.base.BaseService;
import com.hexinfo.dmpro.monitor.dao.ReportManageDao;
import com.hexinfo.dmpro.monitor.model.ReportResourceModel;
import com.hexinfo.dmpro.monitor.model.cm.ClusterResourceInfo;
import com.hexinfo.dmpro.monitor.util.ConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @ClassName: ReportManageService
 * @Description:
 * @Author: lm.cai
 * @Date: 2021-09-16
 */
@Slf4j
@Service("reportManageService")
public class ReportManageService extends BaseService {

    @Resource
    private ReportManageDao reportManageDao;

    @Value("${nowCenterId}")
    private String nowCenterId;

    @Autowired
    private ConvertUtil convertUtil;

    /**
     * Dashboard页 - 服务运行情况展现
     *
     * @return
     */
    //yarn作业运行情况统计
    public Map<String, Object> getYarnCurdateTaskNums(String centerId) {
        Map<String, Object> finaMaps = new HashMap<>();
        List<Map<String, Object>> yarnCurdateTaskNums = reportManageDao.getYarnCurdateTaskNums(centerId);

        List taskList = new ArrayList();
        String[] arrTitle = {"product", "总数", "失败", "正在运行"};
        taskList.add(arrTitle);
        if (yarnCurdateTaskNums != null && yarnCurdateTaskNums.size() > 0) {
            for (Map map : yarnCurdateTaskNums) {
                String[] arr = new String[4];
                arr[0] = map.get("cm_flag").toString();
                arr[1] = map.get("totalNum").toString();
                arr[2] = map.get("failNum").toString();
                arr[3] = map.get("runningNum").toString();
                taskList.add(arr);
            }
        } else {
            String[] arr = new String[4];
            arr[0] = "0";
            arr[1] = "0";
            arr[2] = "0";
            arr[3] = "0";
            taskList.add(arr);
        }
        finaMaps.put("source", taskList);
        return finaMaps;
    }

    //impala作业运行情况统计
    public Map<String, Object> getImpalaCurdateTaskNums(String centerId) {
        Map<String, Object> finaMaps = new HashMap<>();
        List<Map<String, Object>> yarnCurdateTaskNums = reportManageDao.getImpalaCurdateTaskNums(centerId);
        List taskList = new ArrayList();
        String[] arrTitle = {"product", "总数", "失败", "正在运行"};
        taskList.add(arrTitle);
        if (yarnCurdateTaskNums != null && yarnCurdateTaskNums.size() > 0) {
            for (Map map : yarnCurdateTaskNums) {
                String[] arr = new String[4];
                arr[0] = map.get("cm_flag").toString();
                arr[1] = map.get("totalNum").toString();
                arr[2] = map.get("failNum").toString();
                arr[3] = map.get("runningNum").toString();
                taskList.add(arr);
            }
        } else {
            String[] arr = new String[4];
            arr[0] = "0";
            arr[1] = "0";
            arr[2] = "0";
            arr[3] = "0";
            taskList.add(arr);
        }
        finaMaps.put("source", taskList);
        return finaMaps;
    }

    //不同集群在最近24小时内的yarn内存分配率
    public List<ClusterResourceInfo> getResourcefpYarn(String centerId) {
        return reportManageDao.getResourcefpYarn(centerId);
    }


    //不同集群在最近24小时内的yarn内存使用率
    public List<ClusterResourceInfo> getResourceUsageYarn(String centerId) {
        return reportManageDao.getResourceUsageYarn(centerId);
    }

    //不同集群在最近24小时内的Impala内存使用量
    public List<ClusterResourceInfo> getResourceUsageImpala(String centerId) {
        return reportManageDao.getResourceUsageImpala(centerId);

    }

    /**
     * 资源利用率报表页
     *
     * @return
     */
    //查询配置数据
    public Map<String, Object> selAllOptions() {
        Map<String, Object> rspMap = new HashMap<>();
        Map<String, String> reqMap = new HashMap<>();
        reqMap.put("centerId", nowCenterId);
        List<Map<String, String>> cmList = reportManageDao.selAllCm(reqMap);
//        List<Map<String, String>> unitList = reportManageDao.selAllUnit(reqMap);
//        List<Map<String, String>> userList = reportManageDao.selAllUser(reqMap);
        rspMap.put("cmOptions", cmList);
        rspMap.put("queueOptions", null);
//        rspMap.put("userOptions", userList);
//        rspMap.put("unitOptions", unitList);
        return rspMap;
    }

    /**
     * 资源利用率报表页
     *
     * @return
     */
    //查询所有队列
    public Map<String, Object> selAllQueue(ReportResourceModel req) {
        Map<String, Object> rspMap = new HashMap<>();
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("centerId", nowCenterId);
        reqMap.put("cmList", req.getCmList());
        List<Map<String, String>> queueList = null;
        if (req.getMonitorType().equals("YARN")) {
            queueList = reportManageDao.selAllYarnQueue(reqMap);
        } else if (req.getMonitorType().equals("IMPALA")) {
            queueList = reportManageDao.selAllImpalaPool(reqMap);
        } else {

        }
        rspMap.put("queueOptions", queueList);
        return rspMap;
    }

    //查询报表信息
//    public List<ReportResourceModel> selReportInfo(ReportResourceModel req) {
//        req.setCenterId(nowCenterId);
//        List<ReportResourceModel> reportResourceModels = null;
//        String monitorType = req.getMonitorType();
//        String reportType = req.getReportType();
//
//        if (monitorType.equals("YARN")) {
//            reportResourceModels = reportManageDao.selDetailYarnTb(req);
//        } else if (monitorType.equals("IMPALA")) {
//            reportResourceModels = reportManageDao.selDetailImpalaTb(req);
//        } else {
//            reportResourceModels = reportManageDao.selDetailHdfsTb(req);
//        }
//        return reportResourceModels;
//    }
}
